package com.incross.dawin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.LinearLayout;

import com.incross.dawin.util.BKLog;
import com.incross.dawin.views.DawinAdPlayer;
import com.incross.dawin.views.DawinAdPlayer.OnErrorListener;
import com.incross.dawin.views.DawinAdPlayer.OnPlayFinishListener;

public class DawinAdActivity extends Activity {
	
	private static final int ACTION_CLICK = 100;
	private static final int ACTION_CALL = 200;
	
	public static final String KEY_RESULT_CODE = "resultCode";
	
	private int mCurrentAction = -1;
	
	private String mMcd, mPcd, mGcd, mCcd, mScd;
	private boolean mIsRealServer;
	
	
	
	private LinearLayout mMainLayout;
	
	private DawinAdPlayer mDawinPlayer;
	
	private boolean mIsBackKeyEnabled = false;
	
	private boolean mIsVideoPrepared = false;
	
	
	
	private int mFinishTag = -1;
	
	private boolean mIsOtherPageShown = false;
	private int mSelectedAction = -1;
	private boolean mIsPausedBefore = false;
	
	
	private Context mContext;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
//		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		
		Intent intent = getIntent();
		mIsBackKeyEnabled = intent.getBooleanExtra("backKeyEnabled", false);
		mMcd = intent.getStringExtra("mcd");
		mPcd = intent.getStringExtra("pcd");
		mGcd = intent.getStringExtra("gcd");
		mCcd = intent.getStringExtra("ccd");
		mScd = intent.getStringExtra("scd");
		mIsRealServer = intent.getBooleanExtra("realServer", false);
		
		if(mMcd == null || mPcd == null || mGcd == null || mCcd == null || mScd == null) {
			finishAdWithResult(null);
		}
		
		if(mPcd == null) {
			mPcd = "0";
		}
		
		initViews();
		startAd();
	}
	
	@Override
	public void onConfigurationChanged (Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDawinPlayer.onConfigurationChanged();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(mDawinPlayer != null) {
			mDawinPlayer.onResume();
		}
		BKLog.d("OnResume");
		if(mIsOtherPageShown) {
			Intent intent = new Intent();
			if(mCurrentAction == ACTION_CLICK) {
				intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK);
				finishAdWithResult(intent);
			} else if(mCurrentAction == ACTION_CALL) {
				intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_CALL_ACTION);
				finishAdWithResult(intent);
			}
		}
		
		
		if(mIsPausedBefore) {
			finishAdWithResult(null);
			BKLog.d("isPaused before");
		}
	}

	private void initViews() {
		
		mMainLayout = new LinearLayout(mContext);
		LinearLayout.LayoutParams mainParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
		mMainLayout.setOrientation(LinearLayout.VERTICAL);
		
		mainParam.setMargins(0, 0, 0, 0);
		mainParam.gravity = Gravity.CENTER;
		
		mDawinPlayer = new DawinAdPlayer(mContext);
		mDawinPlayer.setDawinServerType((mIsRealServer?DawinConfig.DAWIN_SERVER_REAL:DawinConfig.DAWIN_SERVER_TEST));
		Log.i("DawinAdActivity", "Dawin Ad server " + (mIsRealServer?"REAL":"TEST"));
		LinearLayout.LayoutParams playerParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
		playerParam.gravity = Gravity.CENTER;
		
		mMainLayout.addView(mDawinPlayer, playerParam);
		
		setContentView(mMainLayout, mainParam);
		
		setListeners();
	}
	
	private void startAd() {
		mDawinPlayer.showAd(mGcd, mMcd, mPcd, mScd, mCcd);
	}
	
	@Override
	public void onBackPressed() {
		if(mIsBackKeyEnabled) {
			mDawinPlayer.onBackPressed();
			super.onBackPressed();
		}
	}
	
	@Override
	public void onPause() {
		super.onPause();
		if(mDawinPlayer != null) {
			mDawinPlayer.onPause();
		}
		if(mCurrentAction == -1) {
			mIsPausedBefore = true;
			BKLog.e("maybe exit from application");
		}
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		mDawinPlayer.onDestroy();
	}
	
	
	private void setListeners() {
		if(mDawinPlayer != null) {
			mDawinPlayer.setOnErrorListener(new OnErrorListener() {
				
				@Override
				public void onError(MediaPlayer mp, int what, int extra) {
					mDawinPlayer.destroy();
					Intent intent = new Intent();
					intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_OTHER_ERROR);
					finishAdWithResult(intent);
				}
			});
			
			mDawinPlayer.setOnPlayFinishListener(new OnPlayFinishListener() {
				
				@Override
				public void onPlayFinished(DawinAdPlayer dawinPlayer, int finishedBy) {
					Intent intent = new Intent();
					
					switch(finishedBy) {
						case DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK:
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK);
							mIsOtherPageShown = true;
							mCurrentAction = ACTION_CLICK;
							break;
							
						case DawinAdPlayer.AD_FINISH_ACTION_COMPLETE:
							mDawinPlayer.destroy();
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_ACTION_COMPLETE);
							finishAdWithResult(intent);
							break;
							
						/*case DawinAdPlayer.AD_FINISH_ACTION_SCREEN_CLICK:
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_ACTION_SCREEN_CLICK);
							mIsOtherPageShown = true;
							mCurrentAction = ACTION_CLICK;
							break;*/
							
						case DawinAdPlayer.AD_FINISH_ACTION_SKIP:
							mDawinPlayer.destroy();
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_ACTION_SKIP);
							finishAdWithResult(intent);
							break;
							
						case DawinAdPlayer.AD_FINISH_CALL_ACTION:
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_CALL_ACTION);
							mIsOtherPageShown = true;
							mCurrentAction = ACTION_CALL;
							finishAdWithResult(intent);
							break;
							
						case DawinAdPlayer.AD_FINISH_NO_APPLICABLE_AD:
							mDawinPlayer.destroy();
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_NO_APPLICABLE_AD);
							finishAdWithResult(intent);
							break;
							
						case DawinAdPlayer.AD_FINISH_NETWORK_DISCONNECTED:
							mDawinPlayer.destroy();
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_NETWORK_DISCONNECTED);
							finishAdWithResult(intent);
							break;
							
						case DawinAdPlayer.AD_FINISH_OTHER_ERROR:
							mDawinPlayer.destroy();
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_OTHER_ERROR);
							finishAdWithResult(intent);
							break;
							
						case DawinAdPlayer.AD_FINISH_AD_INFO_RECEIVE_FAIL:
							mDawinPlayer.destroy();
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_AD_INFO_RECEIVE_FAIL);
							finishAdWithResult(intent);
							break;
							
						case DawinAdPlayer.AD_FINISH_BACK_KEY_PRESSED:
							mDawinPlayer.destroy();
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_BACK_KEY_PRESSED);
							finishAdWithResult(intent);
							break;
							
						case DawinAdPlayer.AD_FINISH_AFTER_ACTION:
							mDawinPlayer.destroy();
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_AFTER_ACTION);
							finishAdWithResult(intent);
							break;
							
						default:
							mDawinPlayer.destroy();
							intent.putExtra(KEY_RESULT_CODE, DawinAdPlayer.AD_FINISH_OTHER_ERROR);
							finishAdWithResult(intent);
							break;
					}
					
					
				}
			});
			
			
		}
	}
	
	private void finishAdWithResult(Intent intent) {
		if(intent != null) {
			setResult(RESULT_OK,intent);
		} else {
			setResult(RESULT_OK);
		}
		
		finish();
	}

}
